<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="PuffinPlot" default="default" basedir="."  xmlns:ivy="antlib:org.apache.ivy.ant">
    <description>Builds, tests, and runs the project PuffinPlot.</description>
    <import file="nbproject/build-impl.xml"/>
    <property file="release.properties"/>

    <property name="lib.dir" value="lib" />
    <property name="ivy.install.version" value="2.4.0" />
    <property name="ivy.jar.dir" value="${lib.dir}" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />
        
    <target name="download-ivy" unless="skip.download">
    	<mkdir dir="${ivy.jar.dir}"/>
		<echo message="installing ivy..."/>
    	<get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
    		 dest="${ivy.jar.file}" usetimestamp="true"/>
    </target>
    
    <target name="install-ivy" depends="download-ivy" description="--> install ivy">
      <!-- Remove depends="download-ivy" from the above to build without Internet access -->
        <path id="ivy.lib.path">
            <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
        </path>
        <!-- the loaderref ensures that we use the same loader if multiple
        targets are processed in the same ant run (e.g. "ant clean jar").
        See http://ant.apache.org/ivy/history/latest-milestone/use/settings.html ,
        https://ant.apache.org/manual/Tasks/typedef.html . The value is 
        arbitrary; it just has to be the same for each call. -->
        <taskdef resource="org/apache/ivy/ant/antlib.xml"
                 uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"
                 loaderref="ivy.loader"/>
    </target>
    
    <target name="-pre-init" depends="install-ivy" description="--> retrieve dependencies with ivy">
        <ivy:retrieve type="jar"/>
        <!-- appbundler jar now retrieved so we can safely do the taskdef -->
        <taskdef name="appbundler"
                 classname="com.oracle.appbundler.AppBundlerTask"   
                 classpath="lib/appbundler-1.0ea.jar" />
                            
    <!-- property name="libs.CopyLibs.classpath" value="libs/copylibstask.jar" / -->

    </target>    
    
    <target name="-pre-jar">
        <exec executable="hg" outputproperty="build.hg.revid">
            <arg value="id"/> 
            <arg value="-i"/> 
            <arg value="${basedir}"/>
        </exec>
        <exec executable="hg" outputproperty="build.hg.tag">
            <arg value="id"/> 
            <arg value="-t"/> 
            <arg value="${basedir}"/>
        </exec>
        <exec dir="${basedir}" executable="hg" outputproperty="build.hg.date">
            <arg value="log"/> 
            <arg value="-l1"/> 
            <arg value="-r."/>
            <arg value="--template"/> 
            <arg value="{date|hgdate}"/>
        </exec>
        <propertyfile file="build/classes/net/talvi/puffinplot/build.properties">
            <entry key="build.date" type="date" value="now" pattern="yyyy-MM-dd HH:mm"/>
            <entry key="build.year" type="date" value="now" pattern="yyyy"/>
            <entry key="build.release.version" value="${release.version}"/>
            <entry key="build.hg.revid" value="${build.hg.revid}"/>
            <entry key="build.hg.tag" value="${build.hg.tag}"/>
            <entry key="build.hg.date" value="${build.hg.date}"/>
        </propertyfile>
        
    </target>
    
    <target name="package-for-store" depends="jar"
        description="Creates a composite JAR file containing all dependencies.">
        
        <!-- By default, this target is dependent on NetBeans
        (dependency chain: jar <- -do-jar <- -do-jar-with-libraries <- 
        -do-jar-copylibs)
        because it requires the CopyLibs task, defined in
        org-netbeans-modules-java-j2seproject-copylibstask.jar. To
        allow building with plain Ant, we keep a copy of the jar for
        this task in the PuffinPlot repo and set its path
        in nbproject/project.properties (where it should also override
        the default NetBeans setting).
        Otherwise it would be set to a path in the NetBeans install
        by ~/.netbeans/<version>/build.properties via the untracked
        file nbproject/private/private.properties , making the build
        fail on a clean checkout. -->
        
        <!-- Change the value of this property to be the name of your JAR,
             minus the .jar extension. It should not have spaces.
             <property name="store.jar.name" value="MyJarName"/>
        -->
        <property name="store.jar.name" value="PuffinPlot"/>
        <property name="store.dir" value="dist/single-jar"/>
        <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>

        <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>

        <delete dir="${store.dir}"/>
        <mkdir dir="${store.dir}"/>

        <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
            <zipgroupfileset dir="dist" includes="*.jar"/>
            <zipgroupfileset dir="dist/lib" includes="*.jar"/>

            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>

        <zip destfile="${store.jar}">
            <zipfileset src="${store.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>

        <delete file="${store.dir}/temp_final.jar"/>
    </target>
    
    <!-- macbundle notes:
    Setting the correct name in the application menu title and relevant
    menu items is harder than it seems. The official technique (name
    and displayname properties in the plist) doesn't change the menu
    items (though it does change the title).
    http://comments.gmane.org/gmane.comp.java.openjdk.macosx-port.devel/6737
    mentions three command-line options which can be passed to achieve
    the desired effect; I have successfully tested two of them, and
    use them both here just in case one breaks in a later OS X Java version.
    (For good measure I also set the properties.)
    -->
    
    <target name="macbundle" depends="package-for-store">
        <fail unless="release.version" message="No version number."/>
        <delete dir="${basedir}/dist/${ant.project.name}.app" />
        <tstamp>
            <format property="copyright_msg"
                    pattern="'Copyright 2012-'yyyy' Pontus Lurcock'"
                    locale="en"/>
        </tstamp>
        <appbundler outputdirectory="dist"
            name="${ant.project.name}"
            displayname="${ant.project.name}"
            icon="apple-resources/icons/${ant.project.name}.icns"
            signature="Pont"
            shortversion="${release.version}"
            copyright="${copyright_msg}"
            identifier="net.talvi.puffinplot.PuffinApp"
            mainclassname="net.talvi.puffinplot.PuffinApp">
            <option value="-Xdock:name=${ant.project.name}" />
            <option value="-Dapple.awt.application.name=${ant.project.name}" />
            <classpath file="${basedir}/dist/single-jar/${ant.project.name}.jar" />
        </appbundler>
    </target>
    
    <target name="build-manual">
        <exec dir="${basedir}/manual" executable="${basedir}/manual/make-all.sh" />
    </target>
    
    <target name="create-release" depends="build-manual,macbundle,javadoc"
      description="Creates an archive of binaries and documentation for release">
        <property file="release.properties" />
        <fail unless="release.version" message="No version number."/>
        <property name="release.pathname" value="${basedir}/dist/puffinplot-${release.version}"/>
        <delete dir="${release.pathname}.zip" />
        <zip destfile="${release.pathname}.zip">
            <zipfileset dir="${basedir}/dist/javadoc" prefix="JavaDoc" />
            <zipfileset dir="${basedir}/dist/${ant.project.name}.app"
                prefix="${ant.project.name}.app"
                excludes="Contents/MacOS/JavaAppLauncher"/>
            <zipfileset dir="${basedir}/dist/${ant.project.name}.app"
                prefix="${ant.project.name}.app"
                filemode="755"
                includes="Contents/MacOS/JavaAppLauncher"/>
            <zipfileset file="${basedir}/dist/single-jar/${ant.project.name}.jar"
                filemode="755"/>
            <zipfileset dir="${basedir}/manual" prefix="Manual">
                <include name="manual.pdf" />
                <include name="manual.html" />
                <include name="manual.css" />
                <include name="figures/*.png" />
                <include name="figures/*.jpeg" />
            </zipfileset>
            <zipfileset dir="${basedir}/examples" prefix="Examples"/>
            <zipfileset dir="${basedir}/manual" includes="release-readme.txt" fullpath="README"/>
            <zipfileset dir="${basedir}" includes="LICENCE" fullpath="LICENCE"/>
        </zip>
    </target>

    <!--
    There exist several targets which are by default empty and which can be 
    used for execution of your tasks. These targets are usually executed 
    before and after some main targets. They are: 

      -pre-init:                 called before initialization of project properties
      -post-init:                called after initialization of project properties
      -pre-compile:              called before javac compilation
      -post-compile:             called after javac compilation
      -pre-compile-single:       called before javac compilation of single file
      -post-compile-single:      called after javac compilation of single file
      -pre-compile-test:         called before javac compilation of JUnit tests
      -post-compile-test:        called after javac compilation of JUnit tests
      -pre-compile-test-single:  called before javac compilation of single JUnit test
      -post-compile-test-single: called after javac compilation of single JUunit test
      -pre-jar:                  called before JAR building
      -post-jar:                 called after JAR building
      -post-clean:               called after cleaning build products

    (Targets beginning with '-' are not intended to be called on their own.)

    Example of inserting an obfuscator after compilation could look like this:

        <target name="-post-compile">
            <obfuscate>
                <fileset dir="${build.classes.dir}"/>
            </obfuscate>
        </target>

    For list of available properties check the imported 
    nbproject/build-impl.xml file. 


    Another way to customize the build is by overriding existing main targets.
    The targets of interest are: 

      -init-macrodef-javac:     defines macro for javac compilation
      -init-macrodef-junit:     defines macro for junit execution
      -init-macrodef-debug:     defines macro for class debugging
      -init-macrodef-java:      defines macro for class execution
      -do-jar-with-manifest:    JAR building (if you are using a manifest)
      -do-jar-without-manifest: JAR building (if you are not using a manifest)
      run:                      execution of project 
      -javadoc-build:           Javadoc generation
      test-report:              JUnit report generation

    An example of overriding the target for project execution could look like this:

        <target name="run" depends="PuffinPlot-impl.jar">
            <exec dir="bin" executable="launcher.exe">
                <arg file="${dist.jar}"/>
            </exec>
        </target>

    Notice that the overridden target depends on the jar target and not only on 
    the compile target as the regular run target does. Again, for a list of available 
    properties which you can use, check the target you are overriding in the
    nbproject/build-impl.xml file. 

    -->
</project>
